<include file="header" />
<body>
<div class="wraper">
    <div class="top">
        <a href="{BEESCRM::U('base/slEle/index',array('ele_id'=>$ele_id))}" class="iconfont icon-zuo pull-left"></a>
        <span class="pull-left">立即充值</span>
        <a href="{BEESCRM::U('base/slEle/index',array('ele_id'=>$ele_id))}" class="iconfont icon-shouye pull-right text-right"></a>
    </div>
    <div class="field">请确认下列信息无误</div>
    <div class="info-box">
        <p>充值金额：<span class="price">{BEESCRM:$data.order_amount}</span></p>
        <p>校区：{BEESCRM:$data.community_name}</p>
        <p>楼幢：{BEESCRM:$data.building_name}</p>
        <p>楼层：{BEESCRM:$data.floor_name}</p>
        <p>寝室号：{BEESCRM:$data.room_name}</p>
    </div>
    <div class="btn-box">
	    <input type="hidden" value="{BEESCRM:$ele_id}" id="ele_id">
        <a href="javascript:void(0)" class="btn btn-green sure" data-id="{BEESCRM:$data.order_id}">立即充值</a>
    </div>
</div>
<include file="commonJS" />
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
	$(function() {
		//提交订单
		$(document).on('click','.sure',function(){
			var ele_id = $('#ele_id').val();
			var order_id = $(this).data('id');
			if(!order_id){
				$.alert('error','参数错误');
				return false;
			}

			$.ajax({
				'url': "/base/slEle/orderPay/ele_id/"+ele_id,
				'data': {'order_id':order_id},
				'dataType':'json',
				'type': 'POST',
				'async': false,
				success: function(data) {
					if (data.status == 'S') {
						var wxpay = $.parseJSON(data.wxpay);
						var order_sn = data.order_sn;
						pay(wxpay,order_sn);
					}else{
						$.alert('error',data.message);
						return false;
					}
				}
			});
		});


		//微支付
		function pay(wxpay,order_sn){
			if (typeof WeixinJSBridge == "undefined"){
				if( document.addEventListener ){
					document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
				}else if (document.attachEvent){
					document.attachEvent('WeixinJSBridgeReady', jsApiCall);
					document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
				}
			}else{
				WeixinJSBridge.invoke(
						'getBrandWCPayRequest',
						wxpay,
						function(res){
							WeixinJSBridge.log(res.err_msg);
							if(res.err_msg == "get_brand_wcpay_request:ok"){
								paySuccess(order_sn);
							}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
								$.alert('error','付款已取消');
								return false;
							}else{
								$.alert('error','调用微支付失败');
								return false;
							}
						}
				);
			}
		}

		function paySuccess(order_sn){
			var ele_id = $('#ele_id').val();
			var json = {};
			json.url = "/base/slEle/paysuccess/ele_id/"+ele_id+"/order_sn/"+order_sn;
			$.getAjax(json,false,redirect,'');
		}

		function redirect(data){
			location.href = data.url;
		}

		//页面刷新
		function reload() {
			setTimeout('location.reload()', 2000);
		}
	});
</script>
</body>
</html>